/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.antlr.calc;

import org.antlr.calc.exceptions.RecognitionCalculationException;
import java.util.HashMap;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;

/**
 *
 * @author yana
 */
public class CalculationServiceImpl implements CalculationService {

    private HashMap<String, Object> memory = new HashMap<String, Object>();

    @Override
    public String calculate(String line) {
        CalcLexer lexer = new CalcLexer(new ANTLRStringStream(line));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        CalcParser parser = new CalcParser(tokens);
        parser.setMemory(memory);

        try {
            return parser.prog().toString();
        } catch (RecognitionException ex) {
            throw new RecognitionCalculationException(ex);
        }
    }
}
